package com.zaofan.controller;

import com.zaofan.bean.AccountFromCount;
import com.zaofan.bean.AccountLog;
import com.zaofan.service.AccountFromCountService;
import com.zaofan.utils.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/10/28.
 */

@WebServlet("/accountFrom")
public class AccountFromController extends BaseServlet {
    private static final long serialVersionUID = 1L;
    private AccountFromCountService accountFromCountService = new AccountFromCountService();


    protected void count(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Map<String, Object> params = new HashMap<>();
            Enumeration<String> enumeration = request.getParameterNames();
            while (enumeration.hasMoreElements()) {
                String name = enumeration.nextElement();
                if ("action".equals(name)) {
                    continue;
                }
                Object value = request.getParameter(name);
                params.put(name, value);
            }
            long count = accountFromCountService.findCount(params);

            if (null==params.get("fromtype") || "".equals(params.get("fromtype"))){
                JsonUtils.writeBeanFailJson("fromtype不能为空",response);
                return ;
            }

            if (count==0){
                AccountFromCount accountFromCount  = new AccountFromCount();
                accountFromCount.setCount(1);
                accountFromCount.setFromtype((String) params.get("fromtype"));
                accountFromCountService.add(accountFromCount);
                JsonUtils.writeSuccessPageJson("当前类型无数据自动添加",accountFromCount,count,response);
            }else{
                accountFromCountService.addCount((String) params.get("fromtype"));
                JsonUtils.writeSuccessPageJson("自动添加成功","自动添加成功",count,response);
            }



        } catch (Exception e) {
            e.printStackTrace();
            JsonUtils.writeListFailJson("参数错误!!", response);
        }
        JsonUtils.writeListFailJson("查询失败!!", response);
    }
}
